/*
     File: AtomicElementFlippedView.m
 Abstract: Displays the Atomic Element information with a link to Wikipedia.
  Version: 1.8 
 
 Copyright (C) 2009 Apple Inc. All Rights Reserved.
 
 */
#import "AtomicElementViewController.h"
#import "AtomicElementView.h"
#import "Train.h"
//#import "PeriodicElements.h"
#import "AtomicElementFlippedView.h"
#import <AudioToolbox/AudioToolbox.h>
#import "detailedTimeTable.h"


@implementation AtomicElementFlippedView

@synthesize wikipediaButton;
@synthesize detailedController;

 
-(void)setupUserInterface {
	CGRect buttonFrame = CGRectMake(10.0, 209.0, 234.0, 37.0);
	// create the button
	self.wikipediaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	self.wikipediaButton.frame=buttonFrame;
	
	[self.wikipediaButton setTitle:NSLocalizedString(@"Route", @"") forState:UIControlStateNormal];	
	
	// Center the text on the button, considering the button's shadow
	self.wikipediaButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	self.wikipediaButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	
	[self.wikipediaButton addTarget:self action:@selector(jumpToWikipedia:) forControlEvents:UIControlEventTouchUpInside];

	[self addSubview:self.wikipediaButton];
	return;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) 
	{
		[self setAutoresizesSubviews:YES];
		[self setupUserInterface];
		
		// set the background color of the view to clearn
		self.backgroundColor=[UIColor clearColor];
    }
    return self;
}

- (void)jumpToWikipedia:(id)sender {
	// create the string that points to the correct Wikipedia page for the element name
	//NSString *wikiPageString = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@",element.name];
	/*
	NSString *wikiPageString = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@",@"Test"];
	if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:wikiPageString]])
	{
		// there was an error trying to open the URL. for the moment we'll simply ignore it.
	}
	 */	
	//[((AtomicElementViewController*)self.viewController) goBackInModalDialog]; 
	if(detailedController!=nil){
		//[self.detailedController release];
		self.detailedController=nil;
	}
	
	detailedTimeTable* contr=[[detailedTimeTable alloc] initWithNibName:@"detailedTimeTable" bundle:nil];
	contr.TrainId=element.trainNumber;
	contr.strTitle=element.startStation;
	contr._sDataBaseName=self.viewController.dataBaseName;
	self.detailedController=contr;
	[contr release];	
	[self.detailedController updateData];	
	//[contr updateData];	
	
	[self.viewController.navigationController pushViewController:self.detailedController animated:YES];
}


- (void)drawRect:(CGRect)rect {
	
	// get the background image for the state of the element
	// position it appropriately and draw the image
	UIImage *backgroundImage = [element stateImageForAtomicElementView];
	
	CGRect elementSymbolRectangle = CGRectMake(0,0, [backgroundImage size].width, [backgroundImage size].height);
	[backgroundImage drawInRect:elementSymbolRectangle];
	
	// all the text is drawn in white
	[[UIColor whiteColor] set];	

	// draw the element number	
	UIFont *font = [UIFont boldSystemFontOfSize:32];
	CGPoint point = CGPointMake(10,5);
	
	[[NSString stringWithFormat:@"%@",NSLocalizedString(@"CommentToStation", @"")] 
			drawAtPoint:point withFont:font];
	
	NSString* remarks;
	switch(element.nInfo)
	{
	    case 0:
		remarks= [NSString stringWithString: NSLocalizedString(@"Everyday", @"")];
		break;
	    case 1:
		remarks= [NSString stringWithString: NSLocalizedString(@"WorkingDays", @"")];
		break;
	    case 2:
		remarks= [NSString stringWithString: NSLocalizedString(@"SundaysAndSaturdays", @"")];
		break;

	    case 3:
		remarks= [NSString stringWithString: NSLocalizedString(@"Sundays", @"")];
		break;

	    case 4:
		remarks= [NSString stringWithString: NSLocalizedString(@"Saturdays", @"")];
		break;

	    case 5:
		remarks= [NSString stringWithString: NSLocalizedString(@"FridaysAndSundays", @"")];
		break;

	    case 6:
		remarks= [NSString stringWithString: NSLocalizedString(@"Fridays", @"")];
		break;

	    case 7:
		remarks= [NSString stringWithString: NSLocalizedString(@"FridaysAndSaturdays", @"")];
		break;
	    case 8:
		remarks= [NSString stringWithString: NSLocalizedString(@"FridaysAndSaturdays", @"")];
		break;

	    case 9:
		remarks= [NSString stringWithString: NSLocalizedString(@"SpecialTrain", @"")];
		break;

	    case 10:
		remarks= [NSString stringWithString: NSLocalizedString(@"FridaysSaturdaysAndSundays", @"")];
		break;

	    case 11:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotFriSun", @"")];
		break;

	    case 12:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotWed", @"")];
		break;

	    case 13:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotSunSat", @"")];
		break;
	    case 14:
		remarks= [NSString stringWithString: NSLocalizedString(@"SatAndMon", @"")];
		break;

	    case 15:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotFri", @"")];
		break;

	    case 16:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotSat", @"")];
		break;

	    case 17:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotThursday", @"")];
		break;

	    case 18:
		remarks= [NSString stringWithString: NSLocalizedString(@"NotSun", @"")];
		break;

	    default:
		remarks = [NSString stringWithString: @""];
		break;
	}

	
	NSArray* exceptions=[remarks componentsSeparatedByString: @" "];
	int nCur=80;
	for(id remark in exceptions){
	// then draw it:	
	CGSize stringSize;
	double fStartSize=45.5f;
	fStartSize=[self findMatchSize: remark withStartSize: fStartSize AndBounds: elementSymbolRectangle];
	font = [UIFont boldSystemFontOfSize: fStartSize];
	stringSize = [remark sizeWithFont:font];
	//printf("Found size is: %.2f\n", fStartSize);
	point = CGPointMake((self.bounds.size.width-stringSize.width)/2,256/2-nCur);
	[remark drawAtPoint:point withFont:font];
	nCur-=40;
	}
}

- (void)dealloc {
	[wikipediaButton release];
	[super dealloc];
}

@end
